package com.tjp.eight.ui.fragment.jifen.shareget;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.ToatUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/2/28.
 */

public class FragmentShareGet extends BaseFragment<ShareGetPresenter> implements ShareGetView {

    @Bind(R.id.tv_share_count)
    TextView tv_share_count;
    @Bind(R.id.tv_share_read_count)
    TextView tv_share_read_count;
    @Bind(R.id.tv_share_income)
    TextView tv_share_income;
    @Bind(R.id.tv_tips)
    TextView tv_tips;

    @Override
    protected ShareGetPresenter createPresenter() {
        return new ShareGetPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_share_get, container, false);
        }
        ButterKnife.bind(this, contentView);
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.load(getPJson());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    private String getPJson() {
        ShareGetPModel model = new ShareGetPModel();
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        return new Gson().toJson(model);
    }

    @Override
    public void loadSuccess(SharegetRModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                tv_share_count.setText("" + model.share);
                tv_share_income.setText("" + model.share_income);
                tv_share_read_count.setText("" + model.share_view);
                tv_tips.setText("金币奖励="+"文章阅读数*" + model.share_view_price+"金币");
            } else {
                ToatUtils.toastSuccess(getContext(), model.msg);
            }
        }
    }

    @Override
    public void fail(String msg) {

    }
}
